var system={}
system.data={};

system.Show=function(item){
    system.data=item;
    m.render(document.querySelector(".top"),m(system_top));
    m.render(document.querySelector(".menu"),m(system_menu));
}

var system_top={
    view:function(){
        return [
            m("div.logo",[
                m("span.iconfont.icon-lianjie"),
                system.data.s_project_name
            ]),
            m("div.map"),
            m("div.user",m("button",{onclick:function(){
                location.reload();
            }},"返回"))
        ]
    }
}

var system_menu={
    view:function(){
        return [
            m("button.bt",[
                m("span.iconfont.icon-pdfwendang"),
                "需求清单"
            ]),
            // m("button.bt",[
            //     m("span.iconfont.icon-pdfwendang"),
            //     "数据字典"
            // ]),
            m("button.bt",[
                m("span.iconfont.icon-pdfwendang"),
                "数据库"
            ]),
            m("button.bt",[
                m("span.iconfont.icon-pdfwendang"),
                "接口"
            ]),
        ];
    },
    oncreate:function(){
        document.querySelectorAll(".bt,.bt_active").forEach(bt=>{
            bt.addEventListener("click",function(){
                document.querySelectorAll(".bt,.bt_active").forEach(bt2=>{
                    bt2.setAttribute("class","bt");
                })
                this.setAttribute("class","bt_active");
                index.GoPage(this)
            })
        })
        document.querySelector(".bt").click();
    }
}

var system_demand={
    view:function(){
        return [
            m("div.page_title","需求清单"),
            m("div.page_menu",[
                m("button",{onclick:system_demand_add.Show},"新增"),
                m("button",{onclick:system_demand.Print},"打印")
            ]),
            m("div.page_content.page_content2",m("div.pdfs",system_demand.data.map((item,index)=>{
                if(item.i_drop==1) return;
                return m("table.pdftable",[
                    m("caption",m("a",{href:"#",onclick:function(){
                        system_demand_edit.data=item;
                        system_demand_edit.Show();
                    }},(index+1)+","+item.s_demand_desc)),
                    m("tr",[
                        m("th","表单数据"),
                        m("td",item.s_demand_data)
                    ]),
                    m("tr",[
                        m("th","功能描述"),
                        m("td",m.trust(item.s_demand_list.replace(/\n/g,"<br>")))
                    ])
                ])
            })))
        ]
    }
}
system_demand.Print=function(){
    window.onbeforeprint = function() {
        index.ShowPrint(system.data.s_project_name+"《需求清单》",document.querySelector(".pdfs").outerHTML);
    };
    window.onafterprint = function() {
        index.HidePrint();
    };
    window.print()
}
system_demand.data=[
    {
        "s_demand_desc":"用户维护",
        "s_demand_data":"工号，姓名，性别",
        "s_demand_list":"1.新增用户<br>2.删除用户",
    }
];
system_demand.Show=function(){
    msgbox.Loading();
    m.request({
        method:"GET",
        url:"php/system.php?p=list&sid="+system.data.id+"&s_project_path="+index.rootpath+system.data.s_project_path
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            system_demand.data=res.list;
            m.render(document.querySelector(".content"),m(system_demand));
        }else{
            msgbox.Error(res.message);
        }
    })
}

var system_demand_add={
    view:function(){
        return [
            m("div.page_title","【新增】需求清单"),
            m("div.page_menu",[
                m("button",{onclick:system_demand.Show},"返回")
            ]),
            m("div.page_content",[
                m("div.page_line",[
                    m("div.page_tag","* 需求主题"),
                    m("textarea.page_input#s_demand_desc",{rows:5})
                ]),
                m("div.page_line_br"),
                m("div.page_line",[
                    m("div.page_tag","* 表单数据"),
                    m("textarea.page_input#s_demand_data",{rows:5})
                ]),
                m("div.page_line_br"),
                m("div.page_line",[
                    m("div.page_tag","* 功能描述"),
                    m("textarea.page_input#s_demand_list",{rows:10})
                ])
            ]),
            m("div.page_foot",[
                m("button",{onclick:system_demand_add.Submit},"保存")
            ])
        ]
    }
}
system_demand_add.regex=[
    {key:"s_demand_desc",regex:/^.{1,90}$/,desc:"需求主题字数在1~90之间"},
    {key:"s_demand_data",regex:/^.{1,300}$/,desc:"需表单数据字数在1~90之间"},
    {key:"s_demand_list",regex:/^(.|\n){1,300}$/,desc:"功能描述字数在1~300之间"}
];
system_demand_add.Show=function(){
    m.render(document.querySelector(".content"),m(system_demand_add));
}
system_demand_add.Submit=function(){
    var postData=index.GetPostData(system_demand_add.regex);
    if(!postData){
        msgbox.Error("输入有误，请检查");
        return;
    }
    postData.append("s_project_path",index.rootpath+system.data.s_project_path);
    m.request({
        method:"POST",
        url:"php/system.php?p=add",
        body:postData
    }).then(function(res){
        if(res.res=="OK"){
            system_demand.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}

var system_demand_edit={
    view:function(){
        return [
            m("div.page_title","【更新】需求清单"),
            m("div.page_menu",[
                m("button",{onclick:system_demand.Show},"返回")
            ]),
            m("div.page_content",[
                m("div.page_line",[
                    m("div.page_tag","* 需求主题"),
                    m("textarea.page_input#s_demand_desc",{rows:5,value:system_demand_edit.data.s_demand_desc})
                ]),
                m("div.page_line_br"),
                m("div.page_line",[
                    m("div.page_tag","* 表单数据"),
                    m("textarea.page_input#s_demand_data",{rows:5,value:system_demand_edit.data.s_demand_data})
                ]),
                m("div.page_line_br"),
                m("div.page_line",[
                    m("div.page_tag","* 功能描述"),
                    m("textarea.page_input#s_demand_list",{rows:10,value:system_demand_edit.data.s_demand_list})
                ])
            ]),
            m("div.page_foot",[
                m("button",{onclick:system_demand_edit.Submit},"保存"),
                m("button",{onclick:system_demand_edit.Delete},"删除")
            ])
        ]
    }
}
system_demand_edit.data={};
system_demand_edit.Show=function(){
    m.render(document.querySelector(".content"),m(system_demand_edit));
}
system_demand_edit.Submit=function(){
    var postData=index.GetPostData(system_demand_add.regex);
    if(!postData){
        msgbox.Error("输入有误，请检查");
        return;
    }
    postData.append("id",system_demand_edit.data.id);
    postData.append("s_project_path",index.rootpath+system.data.s_project_path);
    m.request({
        method:"POST",
        url:"php/system.php?p=edit",
        body:postData
    }).then(function(res){
        if(res.res=="OK"){
            system_demand.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}
system_demand_edit.Delete=function(){
    if(confirm("确定删除？")){
        var postData=new FormData();
        postData.append("id",system_demand_edit.data.id);
        postData.append("s_project_path",index.rootpath+system.data.s_project_path);
        m.request({
            method:"POST",
            url:"php/system.php?p=delete",
            body:postData
        }).then(function(res){
            if(res.res=="OK"){
                system.Show();
            }else{
                msgbox.Error(res.message);
            }
        })
    }
}
